package com.odin.rmsservice.controller;

import com.odin.rmsservice.entity.User;
import com.odin.rmsservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 登录控制器
 *
 * @author ice
 * @date 2018-12-04
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录，判定账号密码不能为空、存在且唯一的账号
     */
    @PostMapping
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, Object> map,
                        HttpSession session) {
        if (!StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            User one = userService.getOne(username, password);
            if (one != null) {
                session.setAttribute("loginUser", one.getUsername());
                session.setAttribute("loginEmail", one.getEmail());
                session.setAttribute("loginHeadUrl",one.getHeadUrl());
                return "redirect:/index.html";
            }
            map.put("msg", "账号密码不存在或错误");
            return "login";
        }
        map.put("msg", "账号密码都不能为空");
        return "login";
    }


}
